/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kasgroup2;

import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JComboBox;

/**
 *
 * @author beha
 */
public class UIHotelOverviewForConferenceWithParticipantAndCompanion extends javax.swing.JPanel {

    private Conference selConference = null;
    private Hotel selHotel = null;
    private TableManager tmHotels = null;
    private TableManager tmParticipants = null;
            
    /**
     * Creates new form UIHotelOverviewForConferenceWithParticipantAndCompanion
     */
    public UIHotelOverviewForConferenceWithParticipantAndCompanion() {
        initComponents();
        this.cmbConference.removeAllItems();
        for(Conference c : KASGroup2.getCoreHandlerInstance().getConferenceList()) {
            this.cmbConference.addItem(c.getName());
        }
        tmHotels = new TableManager(this.tblHotels);
        reloadHotelsTable();
        tmParticipants = new TableManager(this.tblParticipants);
        tmParticipants.clear();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblHotels = new javax.swing.JTable();
        pnlHotelDetails = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbConference = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblParticipants = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Hotel Overview for a conference with participants and companion"));

        tblHotels.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"a", null, null, null},
                {"b", null, null, null},
                {"c", null, null, null},
                {"d", null, null, null}
            },
            new String [] {
                "Name", "Address", "Guest count", "Room count"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblHotels.setColumnSelectionAllowed(false);
        tblHotels.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblHotels.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblHotelsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblHotels);
        tblHotels.getColumnModel().getColumn(0).setResizable(false);
        tblHotels.getColumnModel().getColumn(1).setResizable(false);
        tblHotels.getColumnModel().getColumn(1).setPreferredWidth(150);
        tblHotels.getColumnModel().getColumn(2).setResizable(false);
        tblHotels.getColumnModel().getColumn(3).setResizable(false);

        jLabel1.setText("When you select a hotel from the table a panel with hotel details will be shown here.");

        jLabel2.setText("file: UIParticipantWithCompanionHotelOverview.java");

        org.jdesktop.layout.GroupLayout pnlHotelDetailsLayout = new org.jdesktop.layout.GroupLayout(pnlHotelDetails);
        pnlHotelDetails.setLayout(pnlHotelDetailsLayout);
        pnlHotelDetailsLayout.setHorizontalGroup(
            pnlHotelDetailsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlHotelDetailsLayout.createSequentialGroup()
                .addContainerGap(58, Short.MAX_VALUE)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 537, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(44, 44, 44))
            .add(pnlHotelDetailsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlHotelDetailsLayout.createSequentialGroup()
                    .addContainerGap(60, Short.MAX_VALUE)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 537, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(42, 42, 42)))
        );
        pnlHotelDetailsLayout.setVerticalGroup(
            pnlHotelDetailsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlHotelDetailsLayout.createSequentialGroup()
                .add(41, 41, 41)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(pnlHotelDetailsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlHotelDetailsLayout.createSequentialGroup()
                    .addContainerGap(85, Short.MAX_VALUE)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(64, 64, 64)))
        );

        jLabel3.setText("Conference:");

        cmbConference.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbConference.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbConferenceActionPerformed(evt);
            }
        });
        cmbConference.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                cmbConferenceFocusGained(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Participant with companion Hotel Overview for hotel"));

        tblParticipants.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "Room Info", "From", "To", "Has Companion?"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tblParticipants);
        tblParticipants.getColumnModel().getColumn(0).setResizable(false);
        tblParticipants.getColumnModel().getColumn(1).setResizable(false);
        tblParticipants.getColumnModel().getColumn(2).setResizable(false);
        tblParticipants.getColumnModel().getColumn(3).setResizable(false);
        tblParticipants.getColumnModel().getColumn(4).setResizable(false);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, pnlHotelDetails, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cmbConference, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 223, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(cmbConference, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pnlHotelDetails, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void tblHotelsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblHotelsMouseClicked
        this.tmParticipants.clear();
        for(Person p : selConference.getPersonsInHotel(tmHotels.getCellValueForSelectedRow(0))) {
            Boolean hasCompanion = false;
            if(Person.IsParticipant(p)) {
                hasCompanion = ((Participant)p).getHasCompanion();
            }
            this.tmParticipants.addRow(new Object[] { 
                p.getName(), 
                p.getHotelRooms().size(), 
                "",
                "",
                hasCompanion
            });
        }
        /*System.out.println(tblHotels.getSelectedRow());
        pnlHotelDetails.setLayout(new BorderLayout());
        pnlHotelDetails.removeAll();
        pnlHotelDetails.add(new UIParticipantWithCompanionHotelOverview(), java.awt.BorderLayout.CENTER);*/
    }//GEN-LAST:event_tblHotelsMouseClicked

    private void cmbConferenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbConferenceActionPerformed
        Object selConfObj = ((JComboBox)evt.getSource()).getSelectedItem();
        if(selConfObj != null) {
            this.selConference = KASGroup2.getCoreHandlerInstance().getConference(selConfObj.toString());
        }
        if(tmHotels != null) {
            reloadHotelsTable();
        }
    }//GEN-LAST:event_cmbConferenceActionPerformed

    private void cmbConferenceFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmbConferenceFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbConferenceFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbConference;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel pnlHotelDetails;
    private javax.swing.JTable tblHotels;
    private javax.swing.JTable tblParticipants;
    // End of variables declaration//GEN-END:variables

    private void reloadHotelsTable() {
        tmHotels.clear();
        if(selConference != null) {
            for(Hotel h : selConference.getHotelList()) {
                tmHotels.addRow(new Object[] { h.getName(), h.getAddress(), h.getAvailableRoomCount(), h.getRoomCount() });
            }
        }
    }
}
